/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJDialog.java
 *
 * Created on 2014-2-9, 19:23:47
 */
package com.web.ui;

import com.web.server.WEB920Server;
import java.awt.Dimension;
import java.util.Random;

/**
 *
 * @author 920web.com
 */
public class JSplashWindowEx extends javax.swing.JWindow implements Runnable {

    /** Creates new form NewJDialog */
    public JSplashWindowEx() {

        initComponents();
        initData();
    }

        /**
     * 数据初始化
     *
     * @author   于采兴
     */
    private void initData() {

        //设置窗口的大小
        this.setSize(new Dimension(600, 250));

        progress.setStringPainted(true);// 显示百分比字符
        progress.setBorderPainted(false);
        progress.setString("....1....");
        //progress.setBackground( new Color(255, 255, 255));//设置背景色

        Dimension screen = getToolkit().getScreenSize();//得到屏幕尺寸
        pack();//窗口适应组件尺寸
        setLocation((screen.width - getSize().width) / 2, (screen.height - getSize().height) / 2);//设置窗口位置
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        splash = new javax.swing.JPanel();
        progress = new javax.swing.JProgressBar(1, readNum);

        splash.setName("splash"); // NOI18N
        splash.setLayout(null);

        javax.swing.GroupLayout splashLayout = new javax.swing.GroupLayout(splash);
        splash.setLayout(splashLayout);
        splashLayout.setHorizontalGroup(
            splashLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(splashLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)
                .addContainerGap())
        );
        splashLayout.setVerticalGroup(
            splashLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, splashLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26))
        );

        //progress.setBorder(new javax.swing.border.LineBorder(153, 153, 153, 2, true));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(splash, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(splash, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar progress;
    private javax.swing.JPanel splash;
    // End of variables declaration//GEN-END:variables
    //进度条更新线程
    private Thread splashThread = null;
    private int readNum = 50;
    //设置读取配置文件超时时间,单位是钞
    private int overTime = 10;
    // 定义一个线程锁，保证当前只有一个工作在操作中
    private final Object lock = new Object();

    public void start() {
        this.toFront();//窗口前端显示
        splashThread = new Thread(this);//实例化线程
        splashThread.start();//开始运行线程
    }

    @Override
    public void run() {
        this.setVisible(true);
        try {
            int i = 1;
            while(WEB920Server.th_Flag){
            //for (int i = 0; i < readNum; i++) {
                //Thread.sleep(50);//线程休眠
                progress.setValue(progress.getValue() + Math.abs( new Random(50).nextInt())%10);//随着线程进行，增加进度条值

                if (!WEB920Server.th_Flag) {
                    //如果上下文加载完毕后
                    break;
                }
//                if (i > 10 && i < 25) {
//                    progress.setString("....2....");
//                } else if (i > 25 && i < 30) {
//                    progress.setString("....3....");
//                } else if (i > 30 && i < 35) {
//                    progress.setString("....4....");
//                } else if (i > 35) {
//                    progress.setString("....5....");
//                }
                progress.setString("...."+progress.getValue()+"....");
                
//                else if(i>45){
//                    progress.setString(resourceMap.getString("loading.six"));
//                }
                Thread.sleep(210);
                i++;
            }
            progress.setString("....5....");
            progress.setValue(readNum);

        } catch (Exception ex) {
            //LogUtil.saveRunLog(ex);
        }

        showFrame();
    }
    /**
     * 线程监测控制逻辑部分
     */
    private void runInternal() {
        // 无限循环
        progress.setString("....6....");
        for (;;) {
            // 该过程忽略了所有的Exception，以保证线程不会因此而中断
            try {
                synchronized (lock) {
                    try {
                        lock.wait(1000);
                        //System.out.println("time::" + overTime);
                    } catch (Exception e) {
                        // 忽略此Exception
                    }
                    
                    if (!WEB920Server.th_Flag) {
                        break;
                    }

                    if (overTime <= 0) {
                        //系统超时，请稍后再试！
                        System.out.println("系统超时，请稍后再试！");
                        //Dialog.errorMessage("系统超时，请稍后再试！");
                        dispose();//释放窗口
                        System.exit(0);

                    }
                }
                overTime--;

            } catch (Throwable t) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                    // 忽略此Exception
                }
            }
        }
        //如果上下文加载完毕后
        UIApp.run();
        dispose();//释放窗口
    }

    /**
     * 显示系统主框架
     */
    void showFrame() {
System.out.println("--------qqqqqqqqqqqqqqq-------");
        //预防系统超时而滚动窗口无法被关闭
        if (WEB920Server.th_Flag) {
            new Thread(new Runnable() {

                public void run() {
                    runInternal();
                }
            }).start();
            return;
        }
        //如果上下文加载完毕后
        UIApp.run();
        dispose();//释放窗口

    }    
}
